package com.cldz.controller;

import com.cldz.common.dto.req.DevDataDto;
import com.cldz.common.dto.res.DeviceDataResDto;
import com.cldz.service.DevDataService;
import com.cldz.response.support.ServerResponse;
import com.cldz.feign.DevDataFeign;
import com.cldz.utils.TimeBetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @ Author     ：lixiao.
 * @ Date       ：Created in 14:32 2018/12/12
 * @ Description：获取设备数据类接口
 * @ Modified By：
 */
@RestController
@Slf4j
public class DevDataController implements DevDataFeign {
    
    @Autowired
    private DevDataService devDataService;


    /**
     * 获取设备实时数据
     * @param devDataDto
     * @return
     */
    @GetMapping("/deviceRealData")
    public ServerResponse getDeviceRealData(DevDataDto devDataDto){
        List<DeviceDataResDto> deviceRealData = devDataService.getDeviceRealData(devDataDto);
        Integer amount = devDataService.getDeviceRealDataAmount(devDataDto);
        return ServerResponse.createDataAndCount(amount,deviceRealData);
    }

    /**
     * 获取设备历史数据数量
     * @param devDataDto
     * @return
     */
    @GetMapping("/deviceHisData")
    public ServerResponse getHistoryData(DevDataDto devDataDto){
        List<Map<String, Object>> maps = dto2Union(devDataDto);
        Iterator<Map<String, Object>> it = maps.iterator();
        while (it.hasNext()){
            Map<String, Object> next = it.next();
            boolean isExist = devDataService.isTableExist((String) next.get("tableName"));
            if(!isExist){
                it.remove();
            }
        }
        List<DeviceDataResDto> devDataDtos = devDataService.getHistoryData(maps);
        Integer amount = devDataService.getHistoryDataAmount(maps);
        return ServerResponse.createDataAndCount(amount,devDataDtos);
    }

    /**
     * 获取指定设备通道数据
     * @param devDataDto
     * @return
     */
    @GetMapping("/specifyChannelData")
    public ServerResponse getDataByChannelID(DevDataDto devDataDto){
        DevDataDto resDto = devDataService.getDataByChannelID(devDataDto);
        return ServerResponse.createDataList(resDto);
    }

    private final static String HISTORY_TABLE_PREFIX = "t_hisdata";

    /**
     * 历史多表 根据dto转换成对应的list<map>
     * @param devDataDto
     * @return
     */
    private List<Map<String, Object>> dto2Union(DevDataDto devDataDto) {
        String channels = devDataDto.getChannels();
        channels = "'" + channels.replaceAll(",", "','") + "'";

        String startTime = StringUtils.substringBefore(devDataDto.getStartDate()," ").replaceAll("-", "");
        String endTime = StringUtils.substringBefore(devDataDto.getEndDate()," ").replaceAll("-", "");
        List<String> months = TimeBetUtil.getMonths(startTime.substring(0, startTime.length()-2), endTime.substring(0, endTime.length()-2));

        Integer limitBegin = (devDataDto.getCurrentPage() - 1) * devDataDto.getPageSize();
        Integer limitEnd = devDataDto.getPageSize();

        List<Map<String,Object>> list = new ArrayList<>();
        for (String month:months) {
            Map<String,Object> dateMap = new HashMap<>();

            dateMap.put("tableName",HISTORY_TABLE_PREFIX + month);
            dateMap.put("limitBegin",limitBegin);
            dateMap.put("limitEnd",limitEnd);
            dateMap.put("channels",channels);
            dateMap.put("startTime",devDataDto.getStartDate());
            dateMap.put("endTime",devDataDto.getEndDate());
            dateMap.put("conditions",devDataDto.getConditions());
            dateMap.put("devID",devDataDto.getDevID());
            list.add(dateMap);
        }
        return list;
    }
}
